<template>
  <div class="userk">
    <!-- 导航栏 -->
    <van-nav-bar @click-left="onClickLeft" title="个人资料" left-arrow>
    </van-nav-bar>
    <!-- 内容 -->
    <van-cell is-link @click="addPopup"
      >更换头像
      <img class="img" :src="user.headpic" alt="" />
    </van-cell>
    <van-popup
      class="ad"
      v-model="add"
      position="bottom"
      :style="{ height: '30%' }"
    >
      <button class="btn4">
        <van-uploader :after-read="afterRead">
          <van-button @click="onheader">上传头像</van-button>
        </van-uploader>
      </button>
      <button class="btn5 btn4" @click="add = false">取消</button>
    </van-popup>
    <van-cell is-link>
      <div class="nc">
        <div>
          <p>昵称</p>
          <p class="p">每半年只能修改一次</p>
        </div>
        <div>{{ user.uname }}</div>
      </div>
    </van-cell>
    <div class="two">以下信息用于获取精准推荐,计算消耗运动等</div>

    <van-cell is-link @click="sexPopup">
      <div class="text">
        <span>性别</span>
        <span>{{ user.sex == 1 ? "男" : "女" }}</span>
      </div>
    </van-cell>
    <van-popup
      v-model="sex"
      text="111"
      position="bottom"
      :style="{ height: '30%' }"
      right-text="dfsg"
    >
      <van-picker
        title="性别"
        show-toolbar
        :columns="columns"
        @confirm="onSex"
        @cancel="onCancel"
        @change="onChange"
      />
    </van-popup>

    <van-cell is-link @click="birthdayPopup">
      <div class="text">
        <span>生日</span>
        <span>{{ user.birthday }}</span>
      </div>
    </van-cell>
    <van-popup v-model="birthday" position="bottom" :style="{ height: '30%' }">
      <van-datetime-picker
        :value="value"
        v-model="currentDate"
        type="date"
        title="选择年月日"
        @confirm="onConfirm"
        :min-date="minDate"
        @cancel="birthday = false"
        :max-date="maxDate"
      />
    </van-popup>
    <van-cell is-link @click="heightPopup">
      <div class="text">
        <span>身高</span>
        <span>{{ user.height }}</span>
      </div>
    </van-cell>
    <van-popup v-model="height">
      <div class="sg">请输入当前身高</div>
      <span class="span">主动填写视为同意收集该项信息</span>
      <div class="srk"><input class="inp" type="text" v-model="hei" />cm</div>
      <van-button class="btn1" type="primary" @click="onheight" block
        >保存</van-button
      >
      <van-button class="btn2" type="default" @click="height = false"
        >取消</van-button
      >
    </van-popup>
    <van-cell is-link @click="weightPopup">
      <div class="text">
        <span>体重</span>
        <span>{{ user.weight }}</span>
      </div>
    </van-cell>
    <van-popup v-model="weight">
      <div class="sg">请输入当前体重</div>
      <span class="span">主动填写视为同意收集该项信息</span>
      <div class="srk"><input class="inp" type="text" v-model="wei" />kg</div>
      <van-button class="btn1" type="primary" @click="onweight" block
        >保存</van-button
      >
      <van-button class="btn2" type="default" @click="weight = false"
        >取消</van-button
      >
    </van-popup>
    <van-cell is-link @click="targetPopup">
      <div class="text">
        <span>目标体重</span>
        <span>{{ user.targetweight }}</span>
      </div>
    </van-cell>
    <van-popup v-model="target">
      <div class="sg">请输入目标体重</div>
      <span class="span">主动填写视为同意收集该项信息</span>
      <div class="srk"><input class="inp" type="text" v-model="tar" />kg</div>
      <van-button class="btn1" type="primary" @click="ontarget" block
        >保存</van-button
      >
      <van-button class="btn2" type="default" @click="target = false"
        >取消</van-button
      >
    </van-popup>
  </div>
</template>

<script>
import {
  Toast,
  Image as VanImage
} from "vant";
import moment from "moment";
import httpApi from "@/http";
import storage from "@/utils/Storage";
import BaseUrl from "@/http/BaseUrl";
export default {
  data() {
    return {
      tar: "",
      wei: "",
      hei: "",
      add: false,
      sex: false,
      target: false,
      weight: false,
      height: false,
      birthday: false,
      value: "",
      columns: ["女", "男"],
      minDate: new Date(1998, 0, 1),
      maxDate: new Date(2023, 10, 1),
      currentDate: new Date(2000, 0, 17),
      user: {
        uname: "",
        sex: "",
        birthday: "",
        height: "",
        headpic: "",
        weight: "",
        targetweight: "",
      },
      uid:storage.get(storage.KEY_USER).uid,
    };
  },
  mounted() {
    this.userklist();

    console.log(this.user);
  },
  methods: {
    afterRead(file) {
      // 此时可以自行将文件上传至服务器
      console.log(file);

      // 3.限制上传文件的类型，比如只允许图片上传
      // 字符串以 image 开头的正则: /^image/
      if (/^image/.test(file.file.type) == false) {
        alert("只允许上传图片类型的文件");
        return;
      }

      // 文件信息必须 装到 Form 表单类型的数据中 才能上传
      const fd = new FormData(); //表单类型的数据对象
      fd.append("file", file.file);
      console.log(fd);

      $.ajax({
        url:  BaseUrl.UPLOADURL+'/upload',

        type: "POST", //请求方式,必须是post
        // 两个固定设置，与编码类型有关
        processData: false, // 不对内容进行编码，默认会编码成字符串
        contentType: false, // 不更改内容类型，默认会改成字符串模式
        data: fd, // 要上传的数据
        success: (res) => {
          console.log(res);
          this.user.headpic = res.data;
          httpApi.userApi
            .information({
              uid:this.uid,
              headpic: res.data,
            })
            .then((res) => {
              console.log(res);
              this.add = false;
            });
        },
      });
    },
    onheader() {},
    onClickLeft() {
      this.$router.push("/home/my/userc");
    },
    onClickRight() {
      Toast("按钮");
    },
    birthdayPopup() {
      this.birthday = true;
    },
    sexPopup() {
      this.sex = true;
    },
    addPopup() {
      this.add = true;
    },
    heightPopup() {
      this.height = true;
    },
    weightPopup() {
      this.weight = true;
    },
    targetPopup() {
      this.target = true;
    },
    onConfirm(date) {
      console.log(date);
      this.value = moment(date).format("YYYY-MM-DD");
      console.log(this.value);
      this.user.birthday = this.value;
      this.birthday = false;
      httpApi.userApi
        .information({ uid:this.uid, birthday: this.user.birthday })
        .then((res) => {
          console.log("修改", res);
          this.userklist();
        });
    },
    onChange(picker, value, index) {
      this.userklist();
    },
    onSex(value, index) {
      console.log(index);
      httpApi.userApi.information({ uid:this.uid, sex: index }).then((res) => {
        console.log("性别", res);
        this.user.sex = value;
        this.sex = false;
        this.userklist();
      });
    },
    onCancel() {
      Toast("选择性别");
    },
    userklist() {
      httpApi.userApi.userCount({ uid:this.uid }).then((res) => {
        console.log(res);
        this.user = res.data.data[0];
      });
    },
    onheight() {
      console.log(this.hei);
      httpApi.userApi.information({ uid:this.uid, height: this.hei }).then((res) => {
        console.log("身高", res);
        this.user.height = this.hei;
        this.height = false;
      });
    },
    onweight() {
      httpApi.userApi.information({ uid:this.uid, weight: this.wei }).then((res) => {
        console.log("体重", res);
        this.user.weight = this.wei;
        this.weight = false;
      });
    },
    ontarget() {
      httpApi.userApi
        .information({ uid:this.uid, targetweight: this.tar })
        .then((res) => {
          console.log("目标体重", res);
          this.user.targetweight = this.tar;
          this.target = false;
        });
    },
  },
};
</script>

<style scoped>
::v-deep .van-button--default {
  border: none;
}

::v-deep .van-nav-bar .van-icon {
  color: black;
}

::v-deep .van-nav-bar {
  margin-bottom: 10px;
}

::v-deep .van-hairline--bottom::after {
  border: none;
}

.img {
  float: right;
  width: 50px;
  height: 50px;
  border-radius: 25px;
  position: relative;
  right: 10px;
}

.nc {
  display: flex;
  justify-content: space-between;
}

.nc .p {
  font-size: 12px;
  color: #ccc;
}

p {
  margin: 0;
  padding: 0;
}

.two {
  font-size: 12px;
  color: #999;
  padding: 20px 20px 10px 20px;
  background-color: #efe4e490;
}

.userk .van-cell--clickable {
  height: 70px;
}

.userk .van-popup--center {
  width: 200px;
  height: 220px;
  border-radius: 8px;
  padding: 20px 20px 10px 20px;
}

.sg {
  margin-bottom: 15px;
  text-align: center;
  font-weight: 600;
  font-size: 16px;
}

.span {
  font-size: 12px;
  color: #999;
}

.inp {
  display: inline-block;
  width: 30px;
  margin: 30px 5px;
  border: none;
  outline: none;
  border-bottom: 1px solid #999;
  padding: 0px 10px;
}

.srk {
  text-align: center;
}

.btn1 {
  border-radius: 25px;
  height: 40px;
}

.btn2 {
  border: none;
  width: 100%;
  height: 40px;
  margin-top: 10px;
  border-radius: 25px;
}

.text {
  display: flex;
  justify-content: space-between;
}

.add {
  display: flex;
  justify-content: space-between;
  margin: 10px 17px 0px 13px;
}

.add > span {
  width: 80px;
  height: 30px;
  font-size: 14px;
  text-align: center;
  line-height: 30px;
  border-radius: 15px;
  background-color: #ecf8f4;
  color: #7bc0a2;
}

.btn4 {
  background-color: transparent;
  color: #3a3a3a;
  border: none;
  height: 55px;
}

.van-popup--bottom.del {
  height: 17% !important;
}

.van-popup--bottom.ad {
  height: 17% !important;
  display: flex;
  flex-direction: column;
  font-size: 14px;
}

.btn5 {
  background-color: transparent;
  color: #898989;
  border-top: 1px solid #eee;
}
</style>
